//ImageIconProxy.java
package proxy.virtual;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *@author:youtubezou
 *@version:2012-7-26
 */
public class ImagePanelProxy extends JPanel  {
	private static final long serialVersionUID = -4302974086522210771L;
	private ImageIcon imageIcon = null;
	private String imageName;
	@SuppressWarnings("unused")
	private int width;
	@SuppressWarnings("unused")
	private int height;
	boolean isIconCreated = false;
	
	public ImagePanelProxy(String imageName, int width, int height){
		this.imageName = imageName;
		this.width = width;
		this.height = height;
	}
	
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		
		synchronized(this){
			if(imageIcon != null) {
				g.drawImage(imageIcon.getImage(), 0, 0, null);
			} else {
				imageIcon = new ImageIcon(imageName);
				g.drawImage(imageIcon.getImage(), 0, 0, null);
			}
		}
		
		
	}
	
	/**
	 * 
	@Override
	public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
		if(isIconCreated){
			realIcon.paintIcon(c, g, x, y);
			g.drawString("Java and Patterns by youtubezou", x + 20, y + 500);
		} else {
			g.drawRect(x, y, width-1, height-1);
			g.drawString("Loading beautiful girl", x + 20, y + 20);
			
			synchronized(this){
				SwingUtilities.invokeLater(new Runnable(){
					@SuppressWarnings("static-access")
					public void run(){
						try {
								Thread.currentThread().sleep(2000);
								realIcon = new ImageIcon(imageName);
								isIconCreated = true;
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							c.repaint();
					}
				});
			}//synchronized
		}
	}
	

	@Override
	public int getIconWidth() {
		return realIcon.getIconWidth();
	}

	@Override
	public int getIconHeight() {
		return realIcon.getIconHeight();
	}
	*/
}
